package cn.practice.articlepractice01.common.exception;

import cn.practice.articlepractice01.common.response.JsonResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@RestControllerAdvice
@Validated
public class GlobalExceptionHandle {

    @ExceptionHandler
    public JsonResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
        return JsonResult.failed(e.getFieldError().getDefaultMessage());
    }

    @ExceptionHandler
    public JsonResult handleConstraintViolationException(ConstraintViolationException e){
        return JsonResult.failed(e.getMessage());
    }
}
